/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.tools;

import database.DBConPool;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import scripting.NPCConversationManager;
import server.MapleItemInformationProvider;
import server.Start;
/**
 *
 * @author 傻逼疯神
 */
public class 广播系统 extends javax.swing.JFrame {
    /**
     * Creates new form WinStart
     */
    public 广播系统() {
        ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("Image/Icon.png"));
        setIconImage(icon.getImage());
        setTitle("广播系统");
        initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel65 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        广播信息 = new javax.swing.JTable();
        刷新广告 = new javax.swing.JButton();
        发布广告 = new javax.swing.JButton();
        删除广播 = new javax.swing.JButton();
        修改广播 = new javax.swing.JButton();
        广播序号 = new javax.swing.JTextField();
        广播文本 = new javax.swing.JTextField();

        setResizable(false);

        jPanel65.setBorder(javax.swing.BorderFactory.createTitledBorder("广播系统"));

        广播信息.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        广播信息.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "序号", "5分钟一次随机广播内容"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(广播信息);
        if (广播信息.getColumnModel().getColumnCount() > 0) {
            广播信息.getColumnModel().getColumn(0).setMinWidth(100);
            广播信息.getColumnModel().getColumn(0).setMaxWidth(100);
        }

        刷新广告.setFont(new java.awt.Font("幼圆", 1, 15)); // NOI18N
        刷新广告.setText("刷新广播");
        刷新广告.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                刷新广告ActionPerformed(evt);
            }
        });

        发布广告.setFont(new java.awt.Font("幼圆", 1, 15)); // NOI18N
        发布广告.setText("新增广播");
        发布广告.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                发布广告ActionPerformed(evt);
            }
        });

        删除广播.setFont(new java.awt.Font("幼圆", 1, 15)); // NOI18N
        删除广播.setText("删除广播");
        删除广播.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                删除广播ActionPerformed(evt);
            }
        });

        修改广播.setFont(new java.awt.Font("幼圆", 1, 15)); // NOI18N
        修改广播.setText("修改广播");
        修改广播.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                修改广播ActionPerformed(evt);
            }
        });

        广播序号.setEditable(false);
        广播序号.setFont(new java.awt.Font("幼圆", 1, 18)); // NOI18N

        广播文本.setFont(new java.awt.Font("幼圆", 1, 18)); // NOI18N

        javax.swing.GroupLayout jPanel65Layout = new javax.swing.GroupLayout(jPanel65);
        jPanel65.setLayout(jPanel65Layout);
        jPanel65Layout.setHorizontalGroup(
            jPanel65Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addGroup(jPanel65Layout.createSequentialGroup()
                .addComponent(广播序号, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(广播文本))
            .addGroup(jPanel65Layout.createSequentialGroup()
                .addGap(208, 208, 208)
                .addComponent(刷新广告, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(发布广告, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(修改广播, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(删除广播, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(218, Short.MAX_VALUE))
        );
        jPanel65Layout.setVerticalGroup(
            jPanel65Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel65Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel65Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(广播序号, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(广播文本, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel65Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(发布广告, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(删除广播, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(修改广播, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(刷新广告, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 892, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel65, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 410, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel65, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void 刷新广告ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_刷新广告ActionPerformed
        刷新公告广播();
    }//GEN-LAST:event_刷新广告ActionPerformed

    private void 发布广告ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_发布广告ActionPerformed
        if (广播文本.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "请填写广告信息哦。");
            return;
        }
        try (Connection con = DBConPool.getInstance().getDataSource().getConnection(); PreparedStatement ps = con.prepareStatement("INSERT INTO 广播信息 ( 广播 ) VALUES ( ? )")) {
            ps.setString(1, 广播文本.getText());
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(广播系统.class.getName()).log(Level.SEVERE, null, ex);
        }
        刷新公告广播();
        JOptionPane.showMessageDialog(null, "发布完成。");
    }//GEN-LAST:event_发布广告ActionPerformed

    private void 删除广播ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_删除广播ActionPerformed

        PreparedStatement ps1 = null;
        ResultSet rs = null;
        boolean result1 = this.广播序号.getText().matches("[0-9]+");

        if (result1) {
            try {
                ps1 = DBConPool.getInstance().getDataSource().getConnection().prepareStatement("SELECT * FROM 广播信息 WHERE id = ?");
                ps1.setInt(1, Integer.parseInt(this.广播序号.getText()));
                rs = ps1.executeQuery();
                if (rs.next()) {
                    String sqlstr = " delete from 广播信息 where id =" + Integer.parseInt(this.广播序号.getText()) + "";
                    ps1.executeUpdate(sqlstr);
                    刷新公告广播();
                }
            } catch (SQLException ex) {
                Logger.getLogger(广播系统.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_删除广播ActionPerformed

    private void 修改广播ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_修改广播ActionPerformed
        PreparedStatement ps = null;
        PreparedStatement ps1 = null;
        ResultSet rs = null;
        try {
            ps = DBConPool.getInstance().getDataSource().getConnection().prepareStatement("UPDATE 广播信息 SET 广播 = ? WHERE id = ?");
            ps1 = DBConPool.getInstance().getDataSource().getConnection().prepareStatement("SELECT * FROM 广播信息  WHERE id = ? ");
            ps1.setInt(1, Integer.parseInt(广播序号.getText()));
            rs = ps1.executeQuery();
            if (rs.next()) {
                String sqlString1 = null;
                sqlString1 = "update 广播信息 set 广播 = '" + 广播文本.getText() + "' where id = " + Integer.parseInt(广播序号.getText()) + ";";
                PreparedStatement a1 = DBConPool.getInstance().getDataSource().getConnection().prepareStatement(sqlString1);
                a1.executeUpdate(sqlString1);
                刷新公告广播();
                JOptionPane.showMessageDialog(null, "修改成功。");
            }
        } catch (SQLException ex) {
        }
    }//GEN-LAST:event_修改广播ActionPerformed


    private void 刷新公告广播() {
        for (int i = ((DefaultTableModel) (this.广播信息.getModel())).getRowCount() - 1; i >= 0; i--) {
            ((DefaultTableModel) (this.广播信息.getModel())).removeRow(i);
        }
        try {
            Connection con = DBConPool.getInstance().getDataSource().getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;
            ps = con.prepareStatement("SELECT * FROM 广播信息");
            rs = ps.executeQuery();
            while (rs.next()) {
                ((DefaultTableModel) 广播信息.getModel()).insertRow(广播信息.getRowCount(), new Object[]{
                    rs.getString("id"),
                    rs.getString("广播")
                });
            }
        } catch (SQLException ex) {
            Logger.getLogger(广播系统.class.getName()).log(Level.SEVERE, null, ex);
        }
        广播信息.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int i = 广播信息.getSelectedRow();
                String a = 广播信息.getValueAt(i, 0).toString();
                String a1 = 广播信息.getValueAt(i, 1).toString();
                广播序号.setText(a);
                广播文本.setText(a1);
            }
        });

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel65;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton 修改广播;
    private javax.swing.JButton 删除广播;
    private javax.swing.JButton 刷新广告;
    private javax.swing.JButton 发布广告;
    private javax.swing.JTable 广播信息;
    private javax.swing.JTextField 广播序号;
    private javax.swing.JTextField 广播文本;
    // End of variables declaration//GEN-END:variables
}
